#include "junix.h"

int main(int argc, char **argv)
{
	int pfd;
	struct iovec vec;
	struct msghdr msg;
	struct cmsghdr *cmsg;
	int fd=connect_un("hello");

	vec.iov_base = alloca(1024);
	vec.iov_len= 1024;
	msg.msg_name=NULL;
	msg.msg_namelen=0;
	msg.msg_iov=&vec;
	msg.msg_iovlen=1;

	cmsg = alloca(sizeof(struct cmsghdr) + sizeof(int));
    cmsg->cmsg_len=sizeof(struct cmsghdr) + sizeof(int);
	msg.msg_control =cmsg;
	msg.msg_controllen =cmsg->cmsg_len;

	if( ! recvmsg(fd, &msg, 0) )
		err_sys("recvmsg");

	memcpy(&pfd, CMSG_DATA(cmsg),sizeof(pfd));

	//printf("recv file des:%d\n", pfd);
	ssize_t sz;
	char buf[BUFSIZ];

	while( (sz=Read(pfd,buf,BUFSIZ)) > 0)
	   Write(STDOUT_FILENO, buf, sz);

	//Lseek(pfd,0,SEEK_SET);
	Close(fd);
	Close(pfd);
    exit(0);
}

